package com.lancer.backlog.common.impl;

import com.lancer.backlog.common.bean.ResultBean;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.lang.reflect.Method;

/**
 * @author zhaoyue
 * @email 3601704681@qq.com
 * @date Created in  2020/8/20  20:21
 * @description TODO
 */
@ControllerAdvice
public class ResponseResultInterceptor implements ResponseBodyAdvice<Object> {


    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
    }
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass,
                                  ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        Method method=methodParameter.getMethod();
        ResponseResult result = method.getAnnotation(ResponseResult.class);
        if(result!=null){
            ResultBean resultBean=new ResultBean();
            resultBean.setData(body);
            return resultBean;
        }
        return body;
    }
}
